using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace KLib.Input
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    /// 
    [RegistersService(typeof(IInput))]
    public class InputManager : KLib.GameComponent, IInput
    {
        #region Fields
        Mouse mouse = new Mouse();
        GamePad[] gamePads = new GamePad[4];
        #endregion

        #region Properties
        public Mouse Mouse
        {
            get { return mouse; }
        }
        #endregion

        #region Initialization
        public InputManager(Game game)
            : base(game)
        {
            for (int i = 0; i < 4; i++)
            {
                gamePads[i] = new GamePad((PlayerIndex)i);
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();
        }
        #endregion



        #region Methods
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
#if !XBOX
            if (mouse != null)
                mouse.Update(gameTime);
#endif

            for (int i = 0; i < gamePads.Length; i++)
            {
                if (gamePads[i] != null)
                    gamePads[i].Update(gameTime);
            }
            base.Update(gameTime);
        }

        public GamePad GetGamePad(PlayerIndex playerIndex)
        {
            return gamePads[(int)playerIndex];
        }

        public void SetMouseHandle(IntPtr handle)
        {
#if !XBOX
            Microsoft.Xna.Framework.Input.Mouse.WindowHandle = handle;
#endif
        }
        #endregion
    }
}